package com.dotstone.chipism.ui.security.setting;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.dotstone.chipism.R;
import com.dotstone.chipism.activity.BaseActivity;
import com.lib.funsdk.support.FunSupport;
import com.lib.funsdk.support.config.CameraParam;
import com.lib.funsdk.support.models.FunDevice;

public class SettingActivity extends BaseActivity {

    private LinearLayout mLayoutTop = null;
    private ImageView ivHorizontal;
    private ImageView ivVertical;
    private ImageButton mBtnBack = null;

    FunDevice mFunDevice;
    boolean horizontalOpen;
    boolean verticalOpen;

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_camera_setting;
    }

    @Override
    public void initView(View view) {
        mLayoutTop = (LinearLayout)view.findViewById(R.id.layoutTop);
        mLayoutTop.setBackgroundColor(Color.parseColor("#54CBEA"));
        ivHorizontal = (ImageView) view.findViewById(R.id.iv_horizontal);
        ivVertical = (ImageView) view.findViewById(R.id.iv_vertical);
        mBtnBack = (ImageButton) view.findViewById(R.id.backBtnInTopLayout);
        String devMac = getIntent().getStringExtra("FUN_DEVICE_MAC");
        mFunDevice = FunSupport.getInstance().findDeviceBySn(devMac);
        if ( null == mFunDevice ) {
            finish();
            return;
        }
        CameraParam cameraParam = (CameraParam) mFunDevice.getConfig(CameraParam.CONFIG_NAME);
        if (null != cameraParam) {
            // 图像上下翻转
            verticalOpen = cameraParam.getPictureFlip();
            horizontalOpen =cameraParam.getPictureMirror();
            ivVertical.setImageResource(verticalOpen?R.drawable.ic_setting_open:R.drawable.ic_setting_close);

            // 图像左右翻转
            ivHorizontal.setImageResource(horizontalOpen?R.drawable.ic_setting_open:R.drawable.ic_setting_close);
        }
    }

    @Override
    public void setListener() {
        ivVertical.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CameraParam cameraParam = (CameraParam) mFunDevice.getConfig(CameraParam.CONFIG_NAME);
                if (null != cameraParam) {
                    // 图像上下翻转
                    verticalOpen = !verticalOpen;
                    cameraParam.setPictureFlip(verticalOpen);
                    FunSupport.getInstance().requestDeviceSetConfig(mFunDevice, cameraParam);
                    ivVertical.setImageResource(verticalOpen?R.drawable.ic_setting_open:R.drawable.ic_setting_close);
                }
            }
        });
        ivHorizontal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CameraParam cameraParam = (CameraParam) mFunDevice.getConfig(CameraParam.CONFIG_NAME);
                if (null != cameraParam) {
                    // 图像上下翻转
                    horizontalOpen = !horizontalOpen;
                    cameraParam.setPictureMirror(horizontalOpen);
                    FunSupport.getInstance().requestDeviceSetConfig(mFunDevice, cameraParam);
                    ivHorizontal.setImageResource(horizontalOpen?R.drawable.ic_setting_open:R.drawable.ic_setting_close);
                }
            }
        });

        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    public void doBusiness(Context mContext) {

    }


}
